////////////////////////////////////////////////////////////////////////
//
//     Copyright (c) 2009-2013 Denim Group, Ltd.
//
//     The contents of this file are subject to the Mozilla Public License
//     Version 2.0 (the "License"); you may not use this file except in
//     compliance with the License. You may obtain a copy of the License at
//     http://www.mozilla.org/MPL/
//
//     Software distributed under the License is distributed on an "AS IS"
//     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//     License for the specific language governing rights and limitations
//     under the License.
//
//     The Original Code is ThreadFix.
//
//     The Initial Developer of the Original Code is Denim Group, Ltd.
//     Portions created by Denim Group, Ltd. are Copyright (C)
//     Denim Group, Ltd. All Rights Reserved.
//
//     Contributor(s): Denim Group, Ltd.
//
////////////////////////////////////////////////////////////////////////
package com.denimgroup.threadfix.data.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "ChannelVulnerability")
public class ChannelVulnerability extends BaseEntity {

	private static final long serialVersionUID = 2461270529594171704L;

	private ChannelType channelType;
	
	@NotEmpty(message = "{errors.required}")
	@Size(max = 150, message = "{errors.maxlength}")
	private String name;
	
	@NotEmpty(message = "{errors.required}")
	@Size(max = 150, message = "{errors.maxlength}")
	private String code;

	private List<Finding> findings;
	private List<VulnerabilityMap> vulnerabilityMaps;

	@ManyToOne
	@JoinColumn(name = "channelTypeId")
	@JsonIgnore
	public ChannelType getChannelType() {
		return channelType;
	}

	public void setChannelType(ChannelType channelType) {
		this.channelType = channelType;
	}

	@Column(length = 150, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 150, nullable = false)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@OneToMany(mappedBy = "channelVulnerability")
	@JsonIgnore
	public List<Finding> getFindings() {
		return findings;
	}

	public void setFindings(List<Finding> findings) {
		this.findings = findings;
	}

	@OneToMany(mappedBy = "channelVulnerability", cascade = CascadeType.ALL)
	public List<VulnerabilityMap> getVulnerabilityMaps() {
		return vulnerabilityMaps;
	}

	public void setVulnerabilityMaps(List<VulnerabilityMap> vulnerabilityMaps) {
		this.vulnerabilityMaps = vulnerabilityMaps;
	}

	@Transient
	@JsonIgnore
	public GenericVulnerability getGenericVulnerability() {
		if (vulnerabilityMaps != null && vulnerabilityMaps.size() != 0
				&& vulnerabilityMaps.get(0) != null) {
			return vulnerabilityMaps.get(0).getGenericVulnerability();
		} else {
			return null;
		}
	}
}
